package com.umeox.moto.watch.themes.app;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.umeox.moto.watch.themes.skin.entity.DynamicAttr;
import com.umeox.moto.watch.themes.skin.listener.IDynamicNewView;

import org.jetbrains.annotations.NotNull;

import java.util.List;


public abstract class ThemeFragment extends Fragment implements IDynamicNewView {

    private IDynamicNewView mIDynamicNewView;
    protected Context mContext;
    protected static final String TAG = "ThemeFragment";

    private View  rootView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        try {
            mIDynamicNewView = (IDynamicNewView) context;
        } catch (ClassCastException e) {
            mIDynamicNewView = null;
        }
    }

    @NonNull
    @Override
    public LayoutInflater onGetLayoutInflater(@Nullable Bundle savedInstanceState) {
        return super.onGetLayoutInflater(savedInstanceState);
    }


    @NotNull
    @Override
    public LayoutInflater getLayoutInflater( Bundle savedInstanceState) {

        return requireActivity().getLayoutInflater();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(rootView == null){
            rootView = inflater.inflate(getLayoutId(), container, false);
            initView(rootView);
        }
        return rootView;
    }

    @Override
    public void dynamicAddView(View view, List<DynamicAttr> pDAttrs) {
        if (mIDynamicNewView!=null){
            mIDynamicNewView.dynamicAddView(view,pDAttrs);
        }
    }

    protected void initView(View rootView) {

    }

    protected abstract int getLayoutId();


}
